<?php

declare(strict_types=1);

namespace app\admin\middleware;
use think\facade\Log;
use app\utils\Redis as RedisDB;

class PrintLog
{
    /**
     * 验证权限
     *操作类型
     * 1：登录
     * 2：退出
     * 3：菜单路由
     * 4：用户板块
     * 5：角色板块
     * 6：工具标签
     * 7：工具
     * 8：笔记
     * 
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 获取Authorization

        try {
            $logType = $request->rule()->getOption('logType');
            $logContent = $request->rule()->getOption('permission');
            if (empty($logType)) {
                return $next($request);
            }
            $uid = $request->uid;
            $user = RedisDB::hGetAllS("user", "info:$uid");
            
            Log::channel('sys')->record([
                'content' => $logContent,
                'create_by' => $user['username'],
                'log_type' => $logType,
                'ipaddress' => $request->header('x-real-ip')
            ]);
           return $next($request);
        } catch (\Throwable $e) {
            return error('暂无该权限，请联系管理员', '403');
        }
    }

}
